package cn.zbmao.web.util;

import com.jfinal.plugin.activerecord.Record;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 对文本经行高亮处理
 */
public class HighlightToText {
    /**
     *
     * @param text 显示的文本
     * @param keywords 高亮的关键字
     * @return
     * 对文本中存在的关键字经行高亮
     */
	public static String highlight(String text,List<Record> keywords){
		for (Record keyword : keywords) {

            if (keyword.getStr("keyword").length()==1&&macthKeyword(keyword.getStr("keyword"))){
                continue;
            }
			if (text.contains(keyword.getStr("keyword"))) {
				text=text.replace(keyword.getStr("keyword"), "<span class=\'highlight \'>"+keyword.getStr("keyword")+"</span>");
			}
		}


		return text;
	}
//    为字符串数组添加高亮
    public static String highlight(String text,String [] keywords){
        for (String keyword : keywords) {
            keyword=StringOperateUtil.exChange(keyword);
            if (keyword.length()==1&&macthKeyword(keyword)){
                continue;
            }
            if (text.contains(keyword)) {
                text=text.replace(keyword, "<span class=\'highlight \'>"+keyword+"</span>");
            }
        }


        return text;
    }

//    /**
//     *
//     * @param text 正文
//     * @param keywords 关键字数组
//     * @return
//     * 为用户关注的关键字添加高亮，并且添加锚点
//     */
//    public static String highlightSnap(String text,String str){
//        for(int i=0;i<keywords.length;i++){
//            if (keywords[i].length()==1&&macthKeyword(keywords[i])){
//                continue;
//            }
//            if (text.contains(keywords[i])) {
//                if(i==0) {
//                    text = text.replace(keywords[i], "<a name='"+str+"0' style='color:black'><span style='color:black;background-color:#FFBBFF;font-weight:bolder'>" + keywords[i] + "</span></a>");
//                }else if(i==1){
//                    text = text.replace(keywords[i], "<a name='\"+str+\"0' style='color:black'><span style='color:black;background-color:#ffff00;font-weight:bolder'>" + keywords[i]+ "</span></a>");
//                }else if(i==2){
//                    text = text.replace(keywords[i], "<a name='keyword2' style='color:black'><span style='color:black;background-color: chartreuse;font-weight:bolder'>" +keywords[i]+ "</span></a>");
//                }else if(i==3){
//                    text = text.replace(keywords[i], "<a name='keyword3' style='color:black'><span style='color:black;background-color: darkorange;font-weight:bolder'>" +keywords[i] + "</span></a>");
//                }else if(i==4){
//                    text = text.replace(keywords[i], "<a name='keyword4' style='color:black'><span style='color:black;background-color: #2abde9;font-weight:coral'>" +keywords[i]+ "</span></a>");
//                }
//
//            }
//        }
//        return text;
//    }
    public static String highligthQualification(String qualification,String quali) {
        if (StringUtils.isBlank(qualification)){
            return "";
        }
        StringBuffer qualificationStringBuffer = new StringBuffer();




        String[] qualificationStrs = qualification.split(" ");
//       给包含用户设置的资质添加高亮
        for (String qualificationStr : qualificationStrs) {
            if(quali.length()>0&&quali.contains(qualificationStr)){
                qualificationStringBuffer.append("<span class=\'highlight hot\'>"+qualificationStr+"</span>,");
            }else{
                qualificationStringBuffer.append(qualificationStr+",");
            }


        }
        return qualificationStringBuffer.toString().substring(0,qualificationStringBuffer.length()-1);
    }

    /**
     * 对不同关键字添加不同颜色，并添加锚点
     * @param text
     * @param keywords
     * @return
     */
    public static String highlightSnap(String text,List<Record> keywords){
        for(int i=0;i<keywords.size();i++){
            if (keywords.get(i).getStr("keyword").length()==1&&macthKeyword(keywords.get(i).getStr("keyword"))){
                continue;
            }
            if (text.contains(keywords.get(i).getStr("keyword"))) {
                if(i==0) {
                    text = text.replace(keywords.get(i).getStr("keyword"), "<a name='keyword0' style='color:black'><span style='color:black;background-color:#FFBBFF;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                }else if(i==1){
                    text = text.replace(keywords.get(i).getStr("keyword"), "<a name='keyword1' style='color:black'><span style='color:black;background-color:#ffff00;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                }else if(i==2){
                    text = text.replace(keywords.get(i).getStr("keyword"), "<a name='keyword2' style='color:black'><span style='color:black;background-color: chartreuse;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                }else if(i==3){
                    text = text.replace(keywords.get(i).getStr("keyword"), "<a name='keyword3' style='color:black'><span style='color:black;background-color: darkorange;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                }else if(i==4){
                    text = text.replace(keywords.get(i).getStr("keyword"), "<a name='keyword4' style='color:black'><span style='color:black;background-color: #2abde9;font-weight:coral'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                }

            }
        }
        return text;
    }

    /**
     *
     * @param text
     * @param keywords 资质列表
     * @return
     * 为用户关注的资质添加高亮，并且添加锚点
     */
    public static String highlightSnapQuali(String text,List<String> keywords ,String str){
        for(int i=0;i<keywords.size();i++){
            if (keywords.get(i).length()==1&&macthKeyword(keywords.get(i))){
                continue;
            }
            if(str.equals("keyword")){
                if (text.contains(keywords.get(i))) {
                    if (i == 0) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "0' style='color:black'><span style='color:black;background-color:#FFBBFF;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    } else if (i == 1) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "1' style='color:black'><span style='color:black;background-color:#ffff00;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    } else if (i == 2) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "2' style='color:black'><span style='color:black;background-color: chartreuse;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    } else if (i == 3) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "3' style='color:black'><span style='color:black;background-color: darkorange;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    } else if (i == 4) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "4' style='color:black'><span style='color:black;background-color: #2abde9;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    }

                }
            }else {
                if (text.contains(keywords.get(i))) {
                    if (i == 0) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "0' style='color:black'><span style='color:black;background-color:#FFEC8B;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    } else if (i == 1) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "1' style='color:black'><span style='color:black;background-color:#BBFFFF;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    } else if (i == 2) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "2' style='color:black'><span style='color:black;background-color: #98FB98;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    } else if (i == 3) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "3' style='color:black'><span style='color:black;background-color: #7FFF00;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    } else if (i == 4) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "4' style='color:black'><span style='color:black;background-color: #C1FFC1;font-weight:coral'>" + keywords.get(i) + "</span></a>");
                    }

                }
            }
        }
        return text;
    }
    public static List<String> findKey(String text,List<String> keywords,String str){
        List<String>list=new ArrayList<String>();
        for(int i=0;i<keywords.size();i++){

            if (keywords.size()==1&&macthKeyword(keywords.get(i))){
                continue;
            }
            if (text.contains(keywords.get(i))) {
                if(i==0) {
//                    text = text.replace(keywords.get(i).getStr("keyword"), "<a name='keyword0' style='color:black'><span style='color:black;background-color:#FFBBFF;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                    list.add("<a href='"+str+"#keyword0' style='color:black'><span style='color:black;background-color:#FFBBFF;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                }else if(i==1){
                    list.add("<a href='"+str+"#keyword1' style='color:black'><span style='color:black;background-color:#ffff00;font-weight:bolder'>" + keywords.get(i)+ "</span></a>");
                }else if(i==2){
                    list.add("<a href='"+str+"#keyword2' style='color:black'><span style='color:black;background-color: chartreuse;font-weight:bolder'>" +keywords.get(i)+ "</span></a>");
                }else if(i==3){
                    list.add("<a href='"+str+"#keyword3' style='color:black'><span style='color:black;background-color:darkorange;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                }else if(i==4){
                    list.add("<a href='"+str+"#keyword4' style='color:black'><span style='color:black;background-color: #2abde9;font-weight:bolder'>" +keywords.get(i) + "</span></a>");
                }

//               list.add(keywords.get(i).getStr("keyword"));

            }
        }
        return list;
    }

    public static List<String> findQuali(String text,List<String> keywords,String str){
        List<String>list=new ArrayList<String>();
        for(int i=0;i<keywords.size();i++){

            if (keywords.size()==1&&macthKeyword(keywords.get(i))){
                continue;
            }
            if (text.contains(keywords.get(i))) {
                if(i==0) {
//                    text = text.replace(keywords.get(i).getStr("keyword"), "<a name='keyword0' style='color:black'><span style='color:black;background-color:#FFBBFF;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                    list.add("<a href='"+str+"#quali0' style='color:black'><span style='color:black;background-color:#FFEC8B;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                }else if(i==1){
                    list.add("<a href='"+str+"#quali1' style='color:black'><span style='color:black;background-color:#BBFFFF;font-weight:bolder'>" + keywords.get(i)+ "</span></a>");
                }else if(i==2){
                    list.add("<a href='"+str+"#quali2' style='color:black'><span style='color:black;background-color: #98FB98;font-weight:bolder'>" + keywords.get(i)+ "</span></a>");
                }else if(i==3){
                    list.add("<a href='"+str+"#quali3' style='color:black'><span style='color:black;background-color: #7FFF00;font-weight:bolder'>" +  keywords.get(i) + "</span></a>");
                }else if(i==4){
                    list.add("<a href='"+str+"#quali4' style='color:black'><span style='color:black;background-color: #C1FFC1;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                }

//               list.add(keywords.get(i).getStr("keyword"));

            }
        }
        return list;
    }
//    public static String highlightquali(String qualification,String[]quali){
//
//    }

    /**
     * 对关键字添加锚点链接，可跳到对应锚点
     * @param text
     * @param keywords
     * @param id
     * @return
     */
    public static List<String> findKey(String text,List<Record> keywords,int id){
        List<String>list=new ArrayList<String>();
        for(int i=0;i<keywords.size();i++){

            if (keywords.get(i).getStr("keyword").length()==1&&macthKeyword(keywords.get(i).getStr("keyword"))){
                continue;
            }
            if (text.contains(keywords.get(i).getStr("keyword"))) {
                if(i==0) {
//                    text = text.replace(keywords.get(i).getStr("keyword"), "<a name='keyword0' style='color:black'><span style='color:black;background-color:#FFBBFF;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                 list.add("<a href='news/snapShot/"+id+"#keyword0' style='color:black'><span style='color:black;background-color:#FFBBFF;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                }else if(i==1){
                    list.add("<a href='news/snapShot/"+id+"#keyword1' style='color:black'><span style='color:black;background-color:#ffff00;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                }else if(i==2){
                    list.add("<a href='news/snapShot/"+id+"#keyword2' style='color:black'><span style='color:black;background-color: chartreuse;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                }else if(i==3){
                    list.add("<a href='news/snapShot/"+id+"#keyword3' style='color:black'><span style='color:black;background-color: darkorange;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                }else if(i==4){
                    list.add("<a href='news/snapShot/"+id+"#keyword4' style='color:black'><span style='color:black;background-color: #2abde9;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                }

//               list.add(keywords.get(i).getStr("keyword"));

            }
        }
        return list;
    }

//    public static String addMao(String text,List<Record> keywords){
//        for(int i=0;i<keywords.size();i++){
//            if (keywords.get(i).getStr("keyword").length()==1&&macthKeyword(keywords.get(i).getStr("keyword"))){
//                continue;
//            }
//            if (text.contains(keywords.get(i).getStr("keyword"))) {
////                if(text.replaceAll(""))
//
//
//            }
//        }
//        return text;
//    }
	public static String highlightForList(String text,List<String> keywords){
		for (String keyword : keywords) {

            if (keyword.length()==1&&macthKeyword(keyword)){
                continue;
            }
			if (text.contains(keyword)) {
				text=text.replace(keyword, "<span class=\'highlight \'>"+keyword+"</span>");
			}
		}
		return text;
	}
	public static String highlight(String text,String keyword){
        if(keyword!=null) {
            if (text.contains(keyword)) {
                text = text.replace(keyword, "<span class=\'highlight \'>" + keyword + "</span>");
            }
        }
		return text;
	}public static String highlight1(String text,String[] keywords){
        for (String keyword : keywords) {
            if (keyword.length()==1&&macthKeyword(keyword)){
                continue;
            }
            if (keyword != null) {
                if (text.contains(keyword)) {
                    text = text.replace(keyword, "<span style='color:black;background-color:#97FFFF;font-weight:bolder' >" + keyword + "</span>");
                }
            }
        }
        return text;
    }
//    public static String highlight1(String text,String[] keywords){
//        for(int i=0;i<keywords.length;i++){
//            if (keywords[i].length()==1&&macthKeyword(keywords[i])){
//                continue;
//            }
//            if (text.contains(keywords[i])) {
//                if(i==0) {
//                    text = text.replace(keywords[i], "<span style='color:black;background-color:#FFBBFF;font-weight:bolder'>" +keywords[i] + "</span>");
//                }else if(i==1){
//                    text = text.replace(keywords[i], "<span style='color:black;background-color:#ffff00;font-weight:bolder'>" + keywords[i] + "</span>");
//                }else if(i==2){
//                    text = text.replace(keywords[i], "<span style='color:black;background-color: chartreuse;font-weight:bolder'>" + keywords[i] + "</span>");
//                }else if(i==3){
//                    text = text.replace(keywords[i], "<span style='color:black;background-color: darkorange;font-weight:bolder'>" + keywords[i] + "</span>");
//                }else if(i==4){
//                    text = text.replace(keywords[i], "<span style='color:black;background-color: #2abde9;font-weight:coral'>" + keywords[i]+ "</span>");
//                }
//
//            }
//        }
//        return text;
//    }
//判断是否为英文字母
    private static boolean macthKeyword(String keyword){
        Pattern pattern = Pattern.compile("^[a-z]$");
        return pattern.matcher(keyword).find();
    }

}
